package com.xxh.dasongapp.security.config.handler;

import com.xxh.dasongapp.common.Result;
import com.xxh.dasongapp.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class LoginExceptionHandler {

    /**
     * 权限不足异常捕获
     * @param ex
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAccessRE(AccessDeniedException ex){
        log.error("触发异常拦截: " + ex.getMessage(), ex);
        return  Result.error(ResultCode.FORBIDDEN.getCode(),ResultCode.FORBIDDEN.getMsg());
    }



    /**
     * 权限不足异常捕获
     * @param ex
     * @return
     */
    @ExceptionHandler(UsernameNotFoundException.class)
    public Result handleUserNotFound(AccessDeniedException ex){
        log.error("触发异常拦截: " + ex.getMessage(), ex);
        return  Result.error(ResultCode.BAD_REQUEST.getCode(),"用户名或密码错误");
    }
}
